package com.example.friendmanager;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.yanzhenjie.recyclerview.OnItemMenuClickListener;
import com.yanzhenjie.recyclerview.SwipeMenu;
import com.yanzhenjie.recyclerview.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.SwipeMenuItem;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ImageView InageSearch;
    private ImageView InageAdd;
    private SwipeRecyclerView listview;
    private List<ContactModel> data = new ArrayList<>();
    public CustomAdapter adapter;
    private myhelper db = new myhelper(this, myhelper.FRIEND_INFO, null, 1);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initadd();
        listshow();
    }

    private void initView() {//获取组件
        InageSearch = findViewById(R.id.imag_search);
        InageAdd = findViewById(R.id.imag_add);
        listview = findViewById(R.id.list_view);
        adapter = new CustomAdapter(this);
        //SwipeRecyclerView展开方向
        LinearLayoutManager llm = new LinearLayoutManager(this);
        listview.setLayoutManager(llm);

    }

    private void initadd() {//跳转至添加联系人页面
        Intent intent = new Intent(this, AddActivity.class);//跳转
        InageAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(intent);
            }
        });
    }

    public void listshow() {

        SQLiteDatabase database = db.getReadableDatabase();
        if (database.isOpen()) {//数据库打开成功
            //返回游标
            Cursor cursor = database.rawQuery("select * from FRIEND_INFO", null);
            //迭代游标
            while (cursor.moveToNext()){
                ContactModel item = new ContactModel();
                String name = cursor.getString(cursor.getColumnIndex(myhelper.FRIEND_NAME));
                item.setFname(name);
                String sex = cursor.getString(cursor.getColumnIndex(myhelper.FRIEND_SEX));
                if (sex.equals("女")) {
                    item.setFtouxiang(R.drawable.girlpicture);
                } else {
                    item.setFtouxiang(R.drawable.boypicture);
                }
                data.add(item);
            }
            //一定关闭游标
            cursor.close();
            adapter.setData(data);
            listview.setAdapter(adapter);
        }
    }
}